package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author kabuda
 * @create 2022-09-06 10:31
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithDishDto(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page1(int page, int pageSize, String name) {
        //分页查询
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //查询
        dishService.page(pageInfo, queryWrapper);
        //创建一个空查询构造器
        Page<DishDto> dishDtoPage = new Page<>();
        //拷贝，除records属性
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");
        //获得原查询数据的records数据内容
        List<Dish> pageInfoRecords = pageInfo.getRecords();
        //创建一个空list
        List DtoPageInfoRecords = new ArrayList<>();
        //遍历records，将
        for (Dish dish : pageInfoRecords) {
            //根据分类id获得分类名
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            //创建一个dtoDish对象，将dish拷贝过来，并且设置分类名
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            dishDto.setCategoryName(categoryName);
            //将每个dishdto添加到空list
            DtoPageInfoRecords.add(dishDto);
        }
        //设置records
        dishDtoPage.setRecords(DtoPageInfoRecords);

        return R.success(dishDtoPage);

    }

    @GetMapping("/{id}")
    public R<DishDto> getDishById(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.getDishById(id);
        return R.success(dishDto);

    }

    @PutMapping
    public R<String> editDish(@RequestBody DishDto dishDto) {
        dishService.editDishByDishDto(dishDto);
        return R.success("修改成功");
    }


    @DeleteMapping
    public R<String> deleteDish(String ids) {
        String[] split = ids.split(",");
        Long[] idsList = new Long[split.length];
        for (int i = 0; i < split.length; i++) {
            idsList[i] = Long.parseLong(split[i]);
        }
        List<Long> list = Arrays.asList(idsList);
        dishService.removeDish(list);
        return R.success("删除成功");
    }


    //@PostMapping("/status/{status}")
    //public R<String> statusChange(@PathVariable("status")int status ,Long ids){
    //   dishService.statusChange(status,ids);
    //    return R.success("改变状态成功");
    //}
    @PostMapping("/status/{status}")
    public R<String> statusChange(@PathVariable("status") int status, String ids) {
        String[] split = ids.split(",");
        Long[] idsList = new Long[split.length];
        for (int i = 0; i < split.length; i++) {
            idsList[i] = Long.parseLong(split[i]);
        }
        List<Long> list = Arrays.asList(idsList);

        dishService.statusChange(status, list);
        return R.success("改变状态成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId) {
        List<DishDto> list = dishService.getDishByCategoryId(categoryId);
        return R.success(list);
    }


}
